package j3dscene.models;

import com.sun.j3d.utils.image.TextureLoader;
import idk.IDK;
import j3dscene.PrioUserData;
import javax.media.j3d.*;
import javax.swing.JFrame;
import javax.vecmath.Color4f;
import javax.vecmath.Point3f;
import javax.vecmath.TexCoord2f;
import javax.vecmath.Vector3f;

/**
 * Branchgroup of the poster on the walls.
 *
 * @author Dominik K.
 * @author Steffen G.
 */
public class Poster extends BranchGroup {

    /**
     * Path the imagefile which is used as a texture.
     */
    private java.net.URL tex;

    /**
     * Stores the shape.
     *
     * @param texturefile Path to image file.
     */
    public Poster(String texturefile) {
        tex = ClassLoader.getSystemResource(texturefile);
        Node n = buildShape();
        n.setUserData(new PrioUserData("Poster", 100));
        addChild(n);
    }

    /**
     * Creates the shape.
     *
     * @return the shape as a node.
     */
    private Node buildShape() {
        IndexedQuadArray indexedGround = new IndexedQuadArray(8,
                IndexedQuadArray.COORDINATES
                | IndexedQuadArray.NORMALS
                | IndexedQuadArray.TEXTURE_COORDINATE_2, 24);

        Point3f coordinates[] = {
            new Point3f(0.14f, 0.14f, 0.019f),
            new Point3f(-0.14f, 0.14f, 0.019f),
            new Point3f(-0.14f, -0.14f, 0.019f),
            new Point3f(0.14f, -0.14f, 0.019f),};



        Vector3f[] normals = {
            new Vector3f(0.0f, 0.0f, 1.0f),
            new Vector3f(0.0f, 0.0f, -1.0f),};

        TexCoord2f[] textCoord = {
            new TexCoord2f(1.0f, 1.0f),
            new TexCoord2f(0.0f, 1.0f),
            new TexCoord2f(0.0f, 0.0f),
            new TexCoord2f(1.0f, 0.0f)
        };

        int coordIndices[] = {0, 1, 2, 3, 7, 6, 5, 4, 0, 3, 7, 4, 5, 6, 2, 1, 0, 4, 5, 1, 6, 7, 3, 2};
        int normalIndices[] = {0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5};
        int textIndices[] = {0, 1, 2, 3, 3, 0, 1, 2, 1, 2, 3, 0, 1, 2, 3, 0, 3, 0, 1, 2, 1, 2, 3, 0};

        indexedGround.setCoordinates(0, coordinates);
        indexedGround.setCoordinateIndices(0, coordIndices);
        indexedGround.setNormals(0, normals);
        indexedGround.setNormalIndices(0, normalIndices);
        indexedGround.setTextureCoordinates(0, 0, textCoord);
        indexedGround.setTextureCoordinateIndices(0, 0, textIndices);

        return new Shape3D(indexedGround, texture(tex));

    }

    /**
     * Creates an appearance from an image file.
     *
     * @param s Path to image.
     * @return Created appareance.
     */
    private Appearance texture(java.net.URL s) {
        Appearance appearance = new Appearance();
        TextureLoader textureLoader = new TextureLoader(s, (JFrame)IDK.getWindow());
        ImageComponent2D image = textureLoader.getImage();
        Texture2D texture = new Texture2D(Texture.BASE_LEVEL,
                Texture.RGBA,
                image.getWidth(),
                image.getHeight());
        texture.setImage(0, image);
        appearance.setTexture(texture);
        TextureAttributes textureAttr = new TextureAttributes(
                TextureAttributes.REPLACE,
                new Transform3D(),
                new Color4f(),
                TextureAttributes.NICEST);
        appearance.setTextureAttributes(textureAttr);
        Material m = new Material();
        appearance.setMaterial(m);

        return appearance;
    }
}
